package com.think.oauth.user.security.token;

import java.util.Map;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import com.think.oauth.user.service.CustomUserDetailsService;

public class WxCodeWebCustomTokenGranter extends AbstractCustomTokenGranter {

	private CustomUserDetailsService userDetailsService;

    public WxCodeWebCustomTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, CustomUserDetailsService userDetailsService) {
        super(tokenServices, clientDetailsService, requestFactory,"wx_web_code");
        this.userDetailsService = userDetailsService;
    }

    @Override
    protected UserDetails getUserDetails(Map<String, String> parameters) {
        String code = parameters.get("code");
        if (code == null) {
			throw new InvalidGrantException("code 参数丢失");
		}
        return userDetailsService.loadUserByWxCode(code);
    }

}
